from multiprocessing.pool import Pool

from sqlalchemy import create_engine, Column, String, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from get_ip import findip, checkip

# echo=True  开启调试模式
# 参数字符串说明：数据库类型+驱动://用户名:密码@主机:端口号/数据库名字?charset=编码格式
engine = create_engine('数据库类型+驱动://用户名:密码@主机:端口号/数据库名字?charset=utf8')
Base = declarative_base(engine)

# 根据现有的引擎创建会话
session = sessionmaker(engine)()


# 创建数据表
class IP_and_Port(Base):
    __tablename__ = 'IP_and_Port'
    id = Column(Integer, primary_key=True, autoincrement=True)
    ip = Column(String(20))
    port = Column(Integer)

    def __str__(self):
        return f'<IP_and_Port: \t{self.ip}\t{self.port}>'


class insert_MySQL:
    def __init__(self):
        self.session = session
        self.engine = engine

    def add_MySQL(self, other):
        # 新增
        self.session.add(other)
        self.session.commit()


if __name__ == '__main__':
    # Base.metadata.create_all()
    p = Pool(20)
    for i in range(1, 10):
        url = f'https://www.kuaidaili.com/free/inha/{i}'
        print(f'-------------------第{i}页开始-------------', url)
        ip_list = findip(url)
        for item in p.map(checkip, ip_list):
            if item == None:
                pass
            else:
                print('可用的IP' + item)
                a, b = item.split(':')
                ip_port = IP_and_Port
                add_mysql = insert_MySQL()
                add_mysql.add_MySQL(ip_port(ip=a, port=b))
